drop _all

set linesize 200



/* GRAPHS SHOWING ADS BY YEAR */

use ads_by_newspaper_and_year, clear
replace year = year - 1 if mod(year, 2) == 1

collapse (mean) x_* z_* ln_*, by(year)

/* FIGURE 1, TOP LEFT PANEL */

reg ln_x_cand_all year if year <= 1904
predict ln_x_cand_all_hat
scatter ln_x_cand_all ln_x_cand_all_hat year, ylabel(0 .5 1.0 1.5, nogrid) c(l l) s(O i) legend(off) plotregion(style(none)) graphregion(fcolor(white) lcolor(white)) ytitle(Log Ads Per Day) xtitle(Year) title("All Candidates")
graph export ln_cand_all_ads_with_trendline.pdf, replace

/* FIGURE 1, TOP RIGHT PANEL */

reg ln_x_cand_always_p year if year <= 1904
predict ln_x_cand_always_p_hat
scatter ln_x_cand_always_p ln_x_cand_always_p_hat year, ylabel(0 .5 1.0 1.5, nogrid) c(l l) s(O i) legend(off) plotregion(style(none)) graphregion(fcolor(white) lcolor(white)) ytitle(Log Ads Per Day) xtitle(Year) title("Candidates for Always-Partisan Offices")
graph export ln_cand_always_p_ads_with_trendline.pdf, replace

/* FIGURE 1, BOTTOM LEFT PANEL */

reg ln_x_pres_only year if year <= 1904
predict ln_x_pres_only_hat
scatter ln_x_pres_only year, ylabel(0 .25 .5 .75, nogrid) c(l l) s(O i) legend(off) plotregion(style(none)) graphregion(fcolor(white) lcolor(white)) ytitle(Log Ads Per Day) xtitle(Year) title("Presidential Candidates Alone")
graph export ln_pres_only_ads.pdf, replace

/* FIGURE 1, BOTTOM RIGHT PANEL */

reg ln_z_party_only year if year <= 1904
predict ln_z_party_only_hat
scatter ln_z_party_only year, ylabel(0 .25 .5 .75, nogrid) c(l l) s(O i) legend(off) plotregion(style(none)) graphregion(fcolor(white) lcolor(white)) ytitle(Log Ads Per Day) xtitle(Year) title("Party Ads")
graph export ln_party_only_ads.pdf, replace



/* GRAPHS SHOWING ADS BY BEFORE AND AFTER PRIMARIES (FIGURE 2) */

use ads_by_state_and_year, clear
egen x = min(year) if prim_sw == 1, by(state)
egen min_prim_sw = max(x), by(state)
gen t = year - min_prim_sw
replace t = t-1 if mod(t,2) == 1
collapse x_* z_* ln_*, by(t)
drop if abs(t) > 20

/* FIGURE 2, TOP LEFT PANEL */

reg ln_x_cand_all t if t < 0
predict y_neg if e(sample)
reg ln_x_cand_all t if t > 0
predict y_pos if e(sample)
scatter ln_x_cand_all y_neg y_pos t, ylabel(0 .5 1 1.5, nogrid) xlabel(-20 -10 0 10 20) s(O i i) c(. l l) legend(off) plotregion(style(none)) graphregion(fcolor(white) lcolor(white)) xline(0) ytitle(Log Ads Per Day) xtitle(Years Since Statewide Primaries) title(All Candidates)
graph export ln_cand_all_ads_years_since_primary.pdf, replace
drop y_*

/* FIGURE 2, TOP RIGHT PANEL */

reg ln_x_cand_always_p t if t < 0
predict y_neg if e(sample)
reg ln_x_cand_always_p t if t > 0
predict y_pos if e(sample)
scatter ln_x_cand_always_p y_neg y_pos t, ylabel(0 .5 1, nogrid) xlabel(-20 -10 0 10 20) s(O i i) c(. l l) legend(off) plotregion(style(none)) graphregion(fcolor(white) lcolor(white)) yscale(range(0 1.3)) xline(0) ytitle(Log Ads Per Day) xtitle(Years Since Statewide Primaries) title(Candidates for Always-Partisan Offices)
graph export ln_cand_always_p_ads_years_since_primary.pdf, replace
drop y_*

/* FIGURE 2, BOTTOM LEFT PANEL */

reg ln_x_pres_only t if t < 0
predict y_neg if e(sample)
reg ln_x_pres_only t if t > 0
predict y_pos if e(sample)
scatter ln_x_pres_only y_neg y_pos t, ylabel(0 .25 .5 .75, nogrid) xlabel(-20 -10 0 10 20) s(O i i) c(. l l) legend(off) plotregion(style(none)) graphregion(fcolor(white) lcolor(white)) xline(0) ytitle(Log Ads Per Day) xtitle(Years Since Statewide Primaries) title(Presidential Candidates)
graph export ln_pres_only_ads_years_since_primary.pdf, replace
drop y_*

/* FIGURE 2, BOTTOM RIGHT PANEL */

reg ln_z_party_only t if t < 0
predict y_neg if e(sample)
reg ln_z_party_only t if t > 0
predict y_pos if e(sample)
scatter ln_z_party_only y_neg y_pos t, ylabel(0 .25 .5 .75, nogrid) xlabel(-20 -10 0 10 20) s(O i i) c(. l l) legend(off) plotregion(style(none)) graphregion(fcolor(white) lcolor(white)) xline(0) ytitle(Log Ads Per Day) xtitle(Years Since Statewide Primaries) title(Party Ads)
graph export ln_party_only_ads_years_since_primary.pdf, replace
drop y_*



/* GRAPHS SHOWING ADS BY BEFORE AND AFTER AUSTRALIAN BALLOT (FIGURE 3) */

use ads_by_state_and_year, clear
replace partyballot = 1 if state == "MO" & year <= 1920
egen x = min(year) if partyballot == 0, by(state)
egen min_aust_sw = max(x), by(state)
gen t = year - min_aust_sw
replace t = t-1 if mod(t,2) == 1
collapse x_* z_* ln_*, by(t)
drop if abs(t) > 12

/* FIGURE 3, TOP LEFT PANEL */

reg ln_x_cand_all t if t < 0
predict y_neg if e(sample)
reg ln_x_cand_all t if t > 0
predict y_pos if e(sample)
scatter ln_x_cand_all y_neg y_pos t, ylabel(0 .5 1 1.5, nogrid) xlabel(-10 -5 0 5 10) s(O i i) c(. l l) legend(off) plotregion(style(none)) graphregion(fcolor(white) lcolor(white)) xline(0) ytitle(Log Ads Per Day) xtitle(Years Since Australian Ballot Introduced) title(All Candidates)
graph export ln_cand_all_ads_years_since_australian.pdf, replace
drop y_*

/* FIGURE 3, TOP RIGHT PANEL */

reg ln_x_cand_always_p t if t < 0
predict y_neg if e(sample)
reg ln_x_cand_always_p t if t > 0
predict y_pos if e(sample)
scatter ln_x_cand_always_p y_neg y_pos t, ylabel(0 .5 1, nogrid) xlabel(-10 -5 0 5 10) s(O i i) c(. l l) legend(off) plotregion(style(none)) graphregion(fcolor(white) lcolor(white)) xline(0) ytitle(Log Ads Per Day) xtitle(Years Since Australian Ballot Introduced) title(Candidates for Always-Partisan Offices)
graph export ln_cand_always_p_ads_years_since_australian.pdf, replace
drop y_*

/* FIGURE 3, BOTTOM LEFT PANEL */

reg ln_x_pres_only t if t < 0
predict y_neg if e(sample)
reg ln_x_pres_only t if t > 0
predict y_pos if e(sample)
scatter ln_x_pres_only y_neg y_pos t, ylabel(0 .25 .5 .75, nogrid) xlabel(-10 -5 0 5 10) s(O i i) c(. l l) legend(off) plotregion(style(none)) graphregion(fcolor(white) lcolor(white)) xline(0) ytitle(Log Ads Per Day) xtitle(Years Since Australian Ballot Introduced) title(Presidential Candidates)
graph export ln_pres_only_ads_years_since_australian.pdf, replace
drop y_*

/* FIGURE 3, BOTTOM RIGHT PANEL */

reg ln_z_party_only t if t < 0
predict y_neg if e(sample)
reg ln_z_party_only t if t > 0
predict y_pos if e(sample)
scatter ln_z_party_only y_neg y_pos t, ylabel(0 .25 .5 .75, nogrid) xlabel(-10 -5 0 5 10) s(O i i) c(. l l) legend(off) plotregion(style(none)) graphregion(fcolor(white) lcolor(white)) xline(0) ytitle(Log Ads Per Day) xtitle(Years Since Australian Ballot Introduced) title(Party Ads)
graph export ln_party_only_ads_years_since_australian.pdf, replace
drop y_*


/* GRAPHS SHOWING ADS BY BEFORE AND AFTER NON-PARTISAN BALLOT (FIGURE A.3) */

use ads_by_state_and_year, clear
egen x = min(year) if nonp_judge == 1, by(state)
egen min_nonp_judge = max(x), by(state)
gen t = year - min_nonp_judge
replace t = t-1 if mod(t,2) == 1

drop if state == "KS"
drop if state == "MT"
drop if state == "IA" & year >= 1920
drop if state == "PA" & year >= 1922

tab state if t != .

collapse x_* z_* ln_*, by(t)
drop if abs(t) > 20

/* FIGURE A.3 */

reg ln_x_judge_for_np t if t < 0
predict y_neg if e(sample)
reg ln_x_judge_for_np t if t > 0
predict y_pos if e(sample)
scatter ln_x_judge_for_np y_neg y_pos t, ylabel(0 .5 1.0, nogrid) xlabel(-20 -10 0 10 20) s(O i i) c(. l l) legend(off) plotregion(style(none)) graphregion(fcolor(white) lcolor(white)) xline(0) ytitle(Log Ads Per Day) xtitle(Years Since Non-Partisan Ballot) title(Candidates for Judicial Offices)
graph export ln_p_nonp_ads_years_since_nonp_ballot.pdf, replace



/* MAIN REGRESSIONS, AT STATE-YEAR LEVEL AND NEWSPAPER-YEAR LEVEL */

foreach collapse_val in 1 0 {

  if `collapse_val' == 1 {
    local level_var "state"
    use ads_by_state_and_year, clear
  }    

  if `collapse_val' == 0 {
    local subtitle "Analysis at Newspaper-Year Level"
    local level_var "newspaper"
    use ads_by_newspaper_and_year, clear
  }

  tab state, gen(S)
  foreach i of varlist S* {
    gen `i'_t1 = `i' * (year - 1880)
    gen `i'_t2 = `i' * (year - 1880)^2
    drop `i'
  }

  gen byte australian = 1 - partyballot

  gen byte aust_str_tick0 = (australian == 1 & straight_tick == 0)
  gen byte aust_str_tick1 = (australian == 1 & straight_tick == 1)


  save tmp1, replace

  if `collapse_val' == 1 {

    collapse (max) australian prim_sw, by(state year)
    fillin state year
    replace australian = australian[_n-1] if australian == . & state == state[_n-1] & _fillin == 1
    replace prim_sw    = prim_sw[_n-1]    if prim_sw    == . & state == state[_n-1] & _fillin == 1
    collapse (sum) australian prim_sw, by(year)

/* FIGURE A.2 TOP LEFT PANEL */

    scatter prim_sw year, c(stairstep) m(i) ylabel(0 10 20 30, nogrid) legend(off) plotregion(style(none)) graphregion(fcolor(white) lcolor(white)) ytitle(Number of States) xtitle(Year) title(Statewide Primary)
    graph export intro_state_prim.pdf, replace

/* FIGURE A.2 TOP RIGHT PANEL */

    scatter australian year, c(stairstep) m(i) ylabel(0 10 20 30, nogrid) legend(off) plotregion(style(none)) graphregion(fcolor(white) lcolor(white)) ytitle(Number of States) xtitle(Year) title(Australian Ballot)
    graph export intro_aust_ballot.pdf, replace

  }

  use tmp1, clear

  capture log close
  log using regression_results_`level_var'_level.txt, text replace

  xi: areg ln_x_cand_all       prim_sw              i.year,        a(`level_var') cluster(state)  /* FOR TABLE 1 AND APPENDIX TABLE A.3, PANEL 1, COLUMN 1 */
  xi: areg ln_x_cand_all       prim_sw  australian  i.year,        a(`level_var') cluster(state)  /* FOR TABLE 1 AND APPENDIX TABLE A.3, PANEL 1, COLUMN 2 */
  xi: areg ln_x_cand_all       prim_sw              i.year  S*_t1, a(`level_var') cluster(state)  /* FOR TABLE 1 AND APPENDIX TABLE A.3, PANEL 1, COLUMN 3 */
  xi: areg ln_x_cand_all       prim_sw  australian  i.year  S*_t1, a(`level_var') cluster(state)  /* FOR TABLE 1 AND APPENDIX TABLE A.3, PANEL 1, COLUMN 4 */
  xi: areg ln_x_cand_all       prim_sw              i.year  S*_t*, a(`level_var') cluster(state)  /* FOR TABLE 1 AND APPENDIX TABLE A.3, PANEL 1, COLUMN 5 */
  xi: areg ln_x_cand_all       prim_sw  australian  i.year  S*_t*, a(`level_var') cluster(state)  /* FOR TABLE 1 AND APPENDIX TABLE A.3, PANEL 1, COLUMN 6 */

  xi: areg ln_x_cand_always_p  prim_sw              i.year,        a(`level_var') cluster(state)  /* FOR TABLE 1 AND APPENDIX TABLE A.3, PANEL 2, COLUMN 1 */
  xi: areg ln_x_cand_always_p  prim_sw  australian  i.year,        a(`level_var') cluster(state)  /* FOR TABLE 1 AND APPENDIX TABLE A.3, PANEL 2, COLUMN 2 */
  xi: areg ln_x_cand_always_p  prim_sw              i.year  S*_t1, a(`level_var') cluster(state)  /* FOR TABLE 1 AND APPENDIX TABLE A.3, PANEL 2, COLUMN 3 */
  xi: areg ln_x_cand_always_p  prim_sw  australian  i.year  S*_t1, a(`level_var') cluster(state)  /* FOR TABLE 1 AND APPENDIX TABLE A.3, PANEL 2, COLUMN 4 */
  xi: areg ln_x_cand_always_p  prim_sw              i.year  S*_t*, a(`level_var') cluster(state)  /* FOR TABLE 1 AND APPENDIX TABLE A.3, PANEL 2, COLUMN 5 */
  xi: areg ln_x_cand_always_p  prim_sw  australian  i.year  S*_t*, a(`level_var') cluster(state)  /* FOR TABLE 1 AND APPENDIX TABLE A.3, PANEL 2, COLUMN 6 */

  xi: areg ln_x_pres_only      prim_sw              i.year,        a(`level_var') cluster(state)  /* FOR TABLE 1 AND APPENDIX TABLE A.3, PANEL 3, COLUMN 1 */
  xi: areg ln_x_pres_only      prim_sw  australian  i.year,        a(`level_var') cluster(state)  /* FOR TABLE 1 AND APPENDIX TABLE A.3, PANEL 3, COLUMN 2 */
  xi: areg ln_x_pres_only      prim_sw              i.year  S*_t1, a(`level_var') cluster(state)  /* FOR TABLE 1 AND APPENDIX TABLE A.3, PANEL 3, COLUMN 3 */
  xi: areg ln_x_pres_only      prim_sw  australian  i.year  S*_t1, a(`level_var') cluster(state)  /* FOR TABLE 1 AND APPENDIX TABLE A.3, PANEL 3, COLUMN 4 */
  xi: areg ln_x_pres_only      prim_sw              i.year  S*_t*, a(`level_var') cluster(state)  /* FOR TABLE 1 AND APPENDIX TABLE A.3, PANEL 3, COLUMN 5 */
  xi: areg ln_x_pres_only      prim_sw  australian  i.year  S*_t*, a(`level_var') cluster(state)  /* FOR TABLE 1 AND APPENDIX TABLE A.3, PANEL 3, COLUMN 6 */

  xi: areg ln_z_party_only     prim_sw              i.year,        a(`level_var') cluster(state)  /* FOR TABLE 1 AND APPENDIX TABLE A.3, PANEL 4, COLUMN 1 */
  xi: areg ln_z_party_only     prim_sw  australian  i.year,        a(`level_var') cluster(state)  /* FOR TABLE 1 AND APPENDIX TABLE A.3, PANEL 4, COLUMN 2 */
  xi: areg ln_z_party_only     prim_sw              i.year  S*_t1, a(`level_var') cluster(state)  /* FOR TABLE 1 AND APPENDIX TABLE A.3, PANEL 4, COLUMN 3 */
  xi: areg ln_z_party_only     prim_sw  australian  i.year  S*_t1, a(`level_var') cluster(state)  /* FOR TABLE 1 AND APPENDIX TABLE A.3, PANEL 4, COLUMN 4 */
  xi: areg ln_z_party_only     prim_sw              i.year  S*_t*, a(`level_var') cluster(state)  /* FOR TABLE 1 AND APPENDIX TABLE A.3, PANEL 4, COLUMN 5 */
  xi: areg ln_z_party_only     prim_sw  australian  i.year  S*_t*, a(`level_var') cluster(state)  /* FOR TABLE 1 AND APPENDIX TABLE A.3, PANEL 4, COLUMN 6 */


/* WITH ADDED CONTROL FOR NUMBER OF OFFICES UP FOR ELECTION (APPENDIX TABLE A.4) */

  if `collapse_val' == 1 {
    xi: areg ln_x_cand_all       prim_sw  australian  num_offices           i.year,        a(`level_var') cluster(state)  /* FOR APPENDIX TABLE A.4 */
    xi: areg ln_x_cand_all       prim_sw  australian  num_offices           i.year  S*_t1, a(`level_var') cluster(state)  /* FOR APPENDIX TABLE A.4 */
    xi: areg ln_x_cand_all       prim_sw  australian  num_offices           i.year  S*_t*, a(`level_var') cluster(state)  /* FOR APPENDIX TABLE A.4 */
    xi: areg ln_x_cand_always_p  prim_sw  australian  num_offices_partisan  i.year,        a(`level_var') cluster(state)  /* FOR APPENDIX TABLE A.4 */
    xi: areg ln_x_cand_always_p  prim_sw  australian  num_offices_partisan  i.year  S*_t1, a(`level_var') cluster(state)  /* FOR APPENDIX TABLE A.4 */
    xi: areg ln_x_cand_always_p  prim_sw  australian  num_offices_partisan  i.year  S*_t*, a(`level_var') cluster(state)  /* FOR APPENDIX TABLE A.4 */
  }


/* ANALYZE OFFICES NOT DIRECTLY INVOLVED IN PASSING STATE ELECTORAL REFORMS, I.E. OFFICES OTHER THAT STATE LEGISLATURE AND GOVERNOR (APPENDIX TABLE A.5) */

  if `collapse_val' == 1 {
    xi: areg ln_x_cand_not_gov_or_stleg  prim_sw              i.year,        a(`level_var') cluster(state)  /* FOR APPENDIX TABLE A.5 */
    xi: areg ln_x_cand_not_gov_or_stleg  prim_sw  australian  i.year,        a(`level_var') cluster(state)  /* FOR APPENDIX TABLE A.5 */
    xi: areg ln_x_cand_not_gov_or_stleg  prim_sw              i.year  S*_t1, a(`level_var') cluster(state)  /* FOR APPENDIX TABLE A.5 */
    xi: areg ln_x_cand_not_gov_or_stleg  prim_sw  australian  i.year  S*_t1, a(`level_var') cluster(state)  /* FOR APPENDIX TABLE A.5 */
    xi: areg ln_x_cand_not_gov_or_stleg  prim_sw              i.year  S*_t*, a(`level_var') cluster(state)  /* FOR APPENDIX TABLE A.5 */
    xi: areg ln_x_cand_not_gov_or_stleg  prim_sw  australian  i.year  S*_t*, a(`level_var') cluster(state)  /* FOR APPENDIX TABLE A.5 */
  }


/* FOR PRIMARY REFORM, CHECK LATE-MOVING STATES VERSUS EARLY-MOVING STATES (APPENDIX TABLE A.6) */

  if `collapse_val' == 1 {
    egen x = min(year) if prim_sw == 1, by(state)
    egen prim_sw_first_year = max(x), by(state)
    drop x
    gen prim_sw_p0 = prim_sw * (prim_sw_first_year <= 1911)
    gen prim_sw_p1 = prim_sw * (prim_sw_first_year >= 1912)
    xi: areg ln_x_cand_all       prim_sw_p0  prim_sw_p1  i.year,                 a(`level_var') cluster(state)  /* FOR APPENDIX TABLE A.6 */
    xi: areg ln_x_cand_all       prim_sw_p0  prim_sw_p1  i.year if year >= 1890, a(`level_var') cluster(state)  /* FOR APPENDIX TABLE A.6 */
    xi: areg ln_x_cand_always_p  prim_sw_p0  prim_sw_p1  i.year,                 a(`level_var') cluster(state)  /* FOR APPENDIX TABLE A.6 */
    xi: areg ln_x_cand_always_p  prim_sw_p0  prim_sw_p1  i.year if year >= 1890, a(`level_var') cluster(state)  /* FOR APPENDIX TABLE A.6 */
  }


/* ALLOW COEFFICIENT AROUND THE YEAR OF PRIMARY ADOPTION TO VARY OVER TIME, AND LOOK AT LEADS AND LAGS (FIGURE 4) */

  if `collapse_val' == 1 {

    egen x = min(year) if prim_sw == 1, by(state)
    egen year0 = max(x), by(state)
    drop x
    gen prim_sw_pre_14  = (year >= year0 - 4  & year <= year0 - 1)
    gen prim_sw_pre_58  = (year >= year0 - 8  & year <= year0 - 5)
*   gen prim_sw_pre_9x  = (year >= year0 - 50 & year <= year0 - 9)
    gen prim_sw_post_14 = (year >= year0 + 0  & year <= year0 + 3)
    gen prim_sw_post_58 = (year >= year0 + 4  & year <= year0 + 7)
    gen prim_sw_post_9x = (year >= year0 + 8  & year <= year0 + 50)

    gen y = 4*(_n - 3) + 2 in 1/5

    gen b_est = .
    gen lb    = .
    gen ub    = .

/* FIGURE 4, LEFT PANEL */

    xi: areg ln_x_cand_all  prim_sw_pre_* prim_sw_post_*  i.year , a(`level_var') cluster(state)
    replace b_est = _b[prim_sw_pre_58]  in 1
    replace b_est = _b[prim_sw_pre_14]  in 2
    replace b_est = _b[prim_sw_post_14] in 3
    replace b_est = _b[prim_sw_post_58] in 4
    replace b_est = _b[prim_sw_post_9x] in 5
    replace lb = _b[prim_sw_pre_58]  - 2.037 * _se[prim_sw_pre_58]  in 1
    replace lb = _b[prim_sw_pre_14]  - 2.037 * _se[prim_sw_pre_14]  in 2
    replace lb = _b[prim_sw_post_14] - 2.037 * _se[prim_sw_post_14] in 3 
    replace lb = _b[prim_sw_post_58] - 2.037 * _se[prim_sw_post_58] in 4
    replace lb = _b[prim_sw_post_9x] - 2.037 * _se[prim_sw_post_9x] in 5
    replace ub = _b[prim_sw_pre_58]  + 2.037 * _se[prim_sw_pre_58]  in 1
    replace ub = _b[prim_sw_pre_14]  + 2.037 * _se[prim_sw_pre_14]  in 2
    replace ub = _b[prim_sw_post_14] + 2.037 * _se[prim_sw_post_14] in 3
    replace ub = _b[prim_sw_post_58] + 2.037 * _se[prim_sw_post_58] in 4
    replace ub = _b[prim_sw_post_9x] + 2.037 * _se[prim_sw_post_9x] in 5
    replace y     = -10 in 6
    replace b_est =   0 in 6
    replace lb    =   0 in 6
    replace ub    =   0 in 6
    twoway( rspike lb ub y, s(o o) mcolor(gs0)  lcolor(gs0)  lstyle(ci) yline(0) ) ( scatter b_est y, mcolor(gs0) lcolor(gs0) lpattern(solid solid) legend(off) plotregion(style(none)) graphregion(fcolor(white) lcolor(white)) xline(0) yline(0) xlabel(-10 -5 0 5 10) ylabel(-.3 0 .3 .6 .9, nogrid) xtitle("Years Since Primary") ytitle("Coefficient Estimate & Confidence Interval") title("All Candidates") )
    graph export estimates_over_time_all_offices.pdf, replace

/* FIGURE 4, RIGHT PANEL */

    xi: areg ln_x_cand_always_p  prim_sw_pre_* prim_sw_post_*  i.year , a(`level_var') cluster(state)
    replace b_est = _b[prim_sw_pre_58]  in 1
    replace b_est = _b[prim_sw_pre_14]  in 2
    replace b_est = _b[prim_sw_post_14] in 3
    replace b_est = _b[prim_sw_post_58] in 4
    replace b_est = _b[prim_sw_post_9x] in 5
    replace lb = _b[prim_sw_pre_58]  - 2.045 * _se[prim_sw_pre_58]  in 1
    replace lb = _b[prim_sw_pre_14]  - 2.045 * _se[prim_sw_pre_14]  in 2
    replace lb = _b[prim_sw_post_14] - 2.045 * _se[prim_sw_post_14] in 3
    replace lb = _b[prim_sw_post_58] - 2.045 * _se[prim_sw_post_58] in 4
    replace lb = _b[prim_sw_post_9x] - 2.045 * _se[prim_sw_post_9x] in 5
    replace ub = _b[prim_sw_pre_58]  + 2.045 * _se[prim_sw_pre_58]  in 1
    replace ub = _b[prim_sw_pre_14]  + 2.045 * _se[prim_sw_pre_14]  in 2
    replace ub = _b[prim_sw_post_14] + 2.045 * _se[prim_sw_post_14] in 3
    replace ub = _b[prim_sw_post_58] + 2.045 * _se[prim_sw_post_58] in 4
    replace ub = _b[prim_sw_post_9x] + 2.045 * _se[prim_sw_post_9x] in 5
    replace y     = -10 in 6
    replace b_est =   0 in 6
    replace lb    =   0 in 6
    replace ub    =   0 in 6
    twoway( rspike lb ub y, s(o o) mcolor(gs0)  lcolor(gs0)  lstyle(ci) yline(0) ) ( scatter b_est y, mcolor(gs0) lcolor(gs0) lpattern(solid solid) legend(off) plotregion(style(none)) graphregion(fcolor(white) lcolor(white)) xline(0) yline(0) xlabel(-10 -5 0 5 10) ylabel(-.3 0 .3 .6 .9, nogrid) xtitle("Years Since Primary") ytitle("Coefficient Estimate & Confidence Interval") title("Candidates for Always-Partisan Offices") )
    graph export estimates_over_time_always_p.pdf, replace

  }

  log close

}


/* NON-PARTISAN BALLOT REFORM (TABLE 2) */

use ads_by_state_and_year, clear
drop if state == "CT" | state == "DE" | state == "MA" | state == "ME" | state == "NH" | state == "NJ" | state == "RI" | state == "VT"
drop if state == "WI"                   /* SWITCHES TO APRIL ELECTIONS IN ODD YEARS */
drop if state == "KS" & year == 1914    /* SWITCHES TO NON-PARTISAN ELECTIONS FOR JUST ONE YEAR AND ONLY FOR SOME JUDGES */

replace nonp_judge = . if nonp_supreme == . | nonp_oth_jud == .
drop if nonp_judge == .

egen x = count(nonp_judge), by(year)

drop if x <= 3
drop x

gen byte australian = 1 - partyballot

save tmp1, replace


/* FIGURE A.2 BOTTOM PANEL */

collapse (max) nonp_judge, by(state year)
fillin state year
replace nonp_judge = nonp_judge[_n-1] if nonp_judge == . & state == state[_n-1] & _fillin == 1
collapse (sum) nonp_judge, by(year)
scatter nonp_judge year, c(stairstep) m(i) ylabel(0 10 20 30, nogrid) legend(off) plotregion(style(none)) graphregion(fcolor(white) lcolor(white)) ytitle(Number of States) xtitle(Year) title(Non-Partisan Judicial Ballot)
  graph export intro_nonp_judge.pdf, replace


use tmp1, clear

capture log close
log using regression_results_nonpartisan_judge.txt, text replace

xi: areg ln_x_judge_for_np                        nonp_judge  i.year, a(state) cluster(state)  /* FOR TABLE 2 */
xi: areg ln_x_judge_for_np  prim_sw               nonp_judge  i.year, a(state) cluster(state)  /* FOR TABLE 2 */
test prim_sw = nonp_judge
xi: areg ln_x_judge_for_np  prim_sw   australian  nonp_judge  i.year, a(state) cluster(state)  /* FOR TABLE 2 */
test prim_sw = nonp_judge

log close

erase tmp1.dta


